package wenjie.chen.receiver.dao

import org.junit.Before
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import wenjie.chen.receiver.ApplicationTestAware

import static wenjie.chen.receiver.command.fixture.IncreaseAmountCommandFixture.aIncreaseAmountCommand

class AccountDaoTest extends ApplicationTestAware {
    @Autowired
    private AccountDao subject
    private String accountId
    private double remitAmount

    @Before
    void "setup"() {
        def command = aIncreaseAmountCommand().build()
        this.accountId = command.accountId
        this.remitAmount = command.amount
    }

    @Test
    void "TryIncreaseAmount"() {
        def beforeAmount = subject.findAmountBy(accountId)
        def beforeFrozen = subject.findFrozenBy(accountId)

        subject.tryIncreaseAmount(accountId, remitAmount)

        def afterAmount = subject.findAmountBy(accountId)
        def afterFrozen = subject.findFrozenBy(accountId)
        assert afterAmount == beforeAmount
        assert afterFrozen == beforeFrozen + remitAmount
    }

    @Test
    void "ConfirmIncreaseAmount"() {
        def beforeAmount = subject.findAmountBy(accountId)
        def beforeFrozen = subject.findFrozenBy(accountId)

        subject.confirmIncreaseAmount(accountId, remitAmount)

        def afterAmount = subject.findAmountBy(accountId)
        def afterFrozen = subject.findFrozenBy(accountId)
        assert afterAmount == beforeAmount + remitAmount
        assert afterFrozen == beforeFrozen - remitAmount
    }

    @Test
    void "CancelIncreaseAmount"() {
        def beforeAmount = subject.findAmountBy(accountId)
        def beforeFrozen = subject.findFrozenBy(accountId)

        subject.cancelIncreaseAmount(accountId, remitAmount)

        def afterAmount = subject.findAmountBy(accountId)
        def afterFrozen = subject.findFrozenBy(accountId)
        assert afterAmount == beforeAmount
        assert afterFrozen == beforeFrozen - remitAmount
    }

    @Test
    void "TryDecreaseAmount"() {
        def beforeAmount = subject.findAmountBy(accountId)
        def beforeFrozen = subject.findFrozenBy(accountId)

        subject.tryDecreaseAmount(accountId, remitAmount)

        def afterAmount = subject.findAmountBy(accountId)
        def afterFrozen = subject.findFrozenBy(accountId)
        assert afterAmount == beforeAmount - remitAmount
        assert afterFrozen == beforeFrozen + remitAmount
    }

    @Test
    void "ConfirmDecreaseAmount"() {
        def beforeAmount = subject.findAmountBy(accountId)
        def beforeFrozen = subject.findFrozenBy(accountId)

        subject.confirmDecreaseAmount(accountId, remitAmount)

        def afterAmount = subject.findAmountBy(accountId)
        def afterFrozen = subject.findFrozenBy(accountId)
        assert afterAmount == beforeAmount
        assert afterFrozen == beforeFrozen - remitAmount
    }

    @Test
    void "CancelDecreaseAmount"() {
        def beforeAmount = subject.findAmountBy(accountId)
        def beforeFrozen = subject.findFrozenBy(accountId)

        subject.cancelDecreaseAmount(accountId, remitAmount)

        def afterAmount = subject.findAmountBy(accountId)
        def afterFrozen = subject.findFrozenBy(accountId)
        assert afterAmount == beforeAmount + remitAmount
        assert afterFrozen == beforeFrozen - remitAmount
    }
}
